/**
 * 
 * @author Eric Muehlberg
 *
 */


public class player
{
	private static final float maxVelocity = .1f;
	private static final float maxAccelleration = 0.025f;;
	WObject obj;
	boolean moveleft, moveright, falling, jumping;
	private float gravity = 0.098f;
	float jumpTimeMax = 4;
	float currentJumpTime;
	float velocity = 0;
	float accelleration = -.098f;
	//long oldtime;
	
	/**
	 * 
	 * @param o Object used to represent player
	 * 
	 * <Make sure the d component of the object is 0 as the player should be 2D>
	 */
	public player(WObject o)
	{
		obj = o;
		moveleft = false;
		moveright = false;
		falling = true;
		jumping = false;
		//oldtime = 0;
	}
	
	
	//updates using moveleft and moveright booleans
	// needs to be modified to include jumping
	public void update(float time)
	{
		float Hspeed = .05f;
		
		if(moveleft)
		{
			
			switch(obj.getO())
			{
			case xAxis:
				updateX(-Hspeed);
				break;
			case nxAxis:
				updateX(Hspeed);
				break;
			case zAxis:
				updateX(Hspeed);
				break;
			case nzAxis:
				updateX(-Hspeed);
				break;
			}
			
		}
		else if(moveright)
		{
			switch(obj.getO())
			{
			case xAxis:
				updateX(Hspeed);
				break;
			case nxAxis:
				updateX(-Hspeed);
				break;
			case zAxis:
				updateX(-Hspeed);
				break;
			case nzAxis:
				updateX(Hspeed);
				break;
			}
		}
		if (! falling || jumping)
		{
			if(jumping)
			{
				if(currentJumpTime >= 0)
				{
					updateY(velocity);
					currentJumpTime -= .5;
					if(velocity > -gravity)
						velocity -= accelleration;
					else
						velocity = .098f;
					if(accelleration > -gravity)
					{
						accelleration -= gravity;
					}
					else
						accelleration = gravity;
				}
				else
				{
					jumping = false;
					velocity = maxVelocity;
					accelleration = maxAccelleration;
					currentJumpTime = 0;
				}
			}
		}
		else
		{
			updateY(-gravity);
		}
	}
	
	public void jump()
	{
		if(! falling)
		{
			jumping = true;
			currentJumpTime = jumpTimeMax;
			velocity = maxVelocity;
			accelleration = maxAccelleration;
		}
	}


	/**
	 * Changes the characters orientation to the respective plane
	 * via a 90 degree rotation of the level to the left.
	 */
	public void snapLeft()
	{
		((Cube)obj).snapleft();
	}
	
	/**
	 * Changes the characters orientation to the respective plane
	 * via a 90 degree rotation of the level to the right.
	 */
	public void snapRight()
	{
		((Cube)obj).snapright();
	}

//Use the following in the future once perspective shift has been fully configured.

	/**
	 * Returns the relative x coordinate based on perspective
	 * 
	 * Returns x if in one of the X-axis orientations
	 * Returns z if in one of the Z-axis orientations
	 * Else returns 0
	 * 
	 * <Do not assume 0 implies an error>
	 */
	public float getX()
	{
		return obj.getX();
	}
	
	public float getRX()
	{
		return obj.getRX();
	}
	
	public float getY()
	{
		return obj.getY();
	}
	
	public float getZ()
	{
		return obj.getZ();
	}
	
	public float getW()
	{
		return obj.getW();
	}
	
	public float getRW()
	{
		return obj.getRW();
	}
	
	public float getH()
	{
		return obj.getH();
	}
	
	public float getD()
	{
		return obj.getD();
	}
	/**
	 * Updates the relative x coordinate based on perspective
	 * 
	 * Updates x if in one of the X-axis orientations
	 * Updates z if in one of the Z-axis orientations
	 */
	public void updateX(float x)
	{
		obj.updateX(x);
	}
	
	//used to update the height of the player
	private void updateY(float y) {
		obj.updateY(y);
		
	}
	
	public float getRZ()
	{
		return obj.getRZ();
	}
	public void updateRZ(float z)
	{
		obj.updateRZ(z);
	}

}